package com.sc.byahi.user.shared;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.sc.byahi.user.shared.constants.Config;

public class SharedUtil {
	public static void main(String[] args) {
		List<Long> source = new ArrayList<Long>();
		for (int i = 0; i < 61; i++) {
			source.add(new Long(i));
		}
		List<List<Long>> divideList = divideList(source) ;
		System.out.print(divideList(divideList)) ;
		List<Integer> sourcei = new ArrayList<Integer>();
		for (int i = 0; i < 61; i++) {
			sourcei.add(new Integer(i));
		}
		List<List<Integer>> divideListi = divideList(sourcei) ;
		System.out.print(divideList(divideListi)) ;
	}

	public static <T> List<List<T>> divideList(List<T> source) {
		List<List<T>> parts = new ArrayList<List<T>>();
		for (int i = 0;; i = i + Config.QUERY_MAX ) {
			try{
				List<T> subIds = source.subList(i, i + Config.QUERY_MAX);
				if(!subIds.isEmpty())parts.add(subIds);
			}catch(IndexOutOfBoundsException e){
				List<T> subIds = source.subList(i, source.size());
				if(!subIds.isEmpty())parts.add(subIds);
				break;
			}
		}
		return parts;
	}
	public static String getCSV(Collection<String> values) {
		StringBuilder sb = new StringBuilder();
		for (Iterator<String> it = values.iterator(); it.hasNext();) {
			String str = it.next();
			if (!"".equals(str)) {
				sb.append(str);
				if (it.hasNext()) {
					sb.append(", ");
				}
			}
		}
		return sb.toString();
	}

	public static String getCSVIds(List<Long> values) {
		StringBuilder sb = new StringBuilder();
		for (Iterator<Long> it = values.iterator(); it.hasNext();) {
			Long id = it.next();
			sb.append(id);
			if (it.hasNext()) {
				sb.append(",");
			}
		}
		return sb.toString();
	}

	public static Integer getInt(String str) {
		try {
			return Integer.parseInt(str);
		} catch (RuntimeException e) {
			// do nothing
		}
		return null;
	}

	public static Long getLong(String str) {
		try {
			return Long.parseLong(str);
		} catch (RuntimeException e) {
			// do nothing
		}
		return null;
	}

	public static List<Long> getLongList(String str) {
		List<Long> ids = new ArrayList<Long>();
		String[] vals = str.split(",");
		try {
			for (int i = 0; i < vals.length; i++) {
				Long v = getLong(vals[i]);
				if (v != null)
					ids.add(v);
			}
		} catch (RuntimeException e) {
			// do nothing
		}
		return ids;
	}

	public static String fixURL(String url ){
		if (url.contains("w-d-09-SULE")) {
			url = url.replace("w-d-09-SULE", "127.0.0.1");
		}
		if (url.contains("0.0.0.0")) {
			url = url.replace("0.0.0.0", "127.0.0.1");
		}
		if (url.contains("localhost")) {
			url = url.replace("localhost", "127.0.0.1");
		}
		
		return url;
	}
}
